package com.hmyd.hmyd_android.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.Investment;
import com.hmyd.hmyd_android.ui.InvestmentDetailActivity;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.HomeSeekArc;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapCommonUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;

public class InvestmentListAdapter extends BaseAdapter {
	private Context mContext;
	private List<Investment> mPlist;
	private LayoutInflater mInflater;
	BitmapUtils bitmapUtils;
	BitmapDisplayConfig displayConfig;

	public InvestmentListAdapter(Context context, List<Investment> plist) {
		super();

		bitmapUtils = new BitmapUtils(context);
		displayConfig = new BitmapDisplayConfig();
		displayConfig.setShowOriginal(false);
		displayConfig.setBitmapMaxSize(BitmapCommonUtils.getScreenSize(context));

		this.mContext = context;
		this.mPlist = plist;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return mPlist != null ? mPlist.size() : 0;
	}

	@Override
	public Investment getItem(int position) {
		return mPlist != null ? mPlist.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (mPlist == null || getItem(position) == null) {
			return convertView;
		}

		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_investment_list_item, null);
			holder = new ViewHolder();
			holder.type = (TextView) convertView.findViewById(R.id.type);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.tag_image = (ImageView) convertView.findViewById(R.id.tag_image);

			holder.investment_rate = (TextView) convertView.findViewById(R.id.rate);
			holder.investment_rate_sp = (TextView) convertView.findViewById(R.id.rate_sp);
			holder.investment_rate_tag = (TextView) convertView.findViewById(R.id.rate_tag);
			
			holder.investment_period = (TextView) convertView.findViewById(R.id.period);

			holder.tags_layout_par = (FrameLayout) convertView.findViewById(R.id.tags_layout_par);
			holder.tags_layout = (HomeSeekArc) convertView.findViewById(R.id.tags_layout);
			holder.tags_label = (TextView) convertView.findViewById(R.id.tags_label);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Investment investment = getItem(position);
		if (investment != null) {

			if (investment.type.equals("shf")) {
				holder.type.setText("首惠丰");
				holder.type.setTextColor(mContext.getResources().getColor(R.color.text_orange));

				holder.type.setBackgroundDrawable(mContext.getResources().getDrawable(
						R.drawable.shape_shf_bg));
			} else if (investment.type.equals("phb")) {
				holder.type.setText("普惠保");
				holder.type.setTextColor(mContext.getResources().getColor(R.color.pro_type_txt));

				holder.type.setBackgroundDrawable(mContext.getResources().getDrawable(
						R.drawable.shape_pu_bg));
			} else if (investment.type.equals("hht")) {
				holder.type.setText("鸿惠通");
				holder.type.setTextColor(mContext.getResources().getColor(R.color.holo_red_dark));

				holder.type.setBackgroundDrawable(mContext.getResources().getDrawable(
						R.drawable.shape_hht_bg));
			} else {
				holder.type.setText("");
			}

			holder.title.setText(investment.title);

			getRateView(investment, holder);
			
			holder.investment_period.setText(String.valueOf(investment.period) + Utils.periodConversion(investment.periodUnit));
			holder.investment_period.setTextColor(mContext.getResources().getColor(R.color.text_orange));

			//			holder.time.setText(investment.period + "");
			//			holder.time_unit.setText(Utils.periodConversion(investment.periodUnit));

			holder.tags_layout_par.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mContext.startActivity(InvestmentDetailActivity.getIntent(mContext,
							getItem(position)));
				}
			});

			holder.investment_rate
			.setTextColor(mContext.getResources().getColor(R.color.text_orange));
			holder.investment_rate_sp.setTextColor(mContext.getResources().getColor(
					R.color.text_orange));
			holder.investment_rate_tag.setTextColor(mContext.getResources().getColor(
					R.color.text_orange));

			holder.tags_layout_par.setEnabled(true);

			holder.tags_layout.setVisibility(View.VISIBLE);
			holder.tags_layout.setArcFillColor(mContext.getResources().getColor(R.color.white));
			holder.tags_layout.setProgress(Integer.parseInt(investment.progress), false);
			holder.tags_layout.setProgressWidth(1.5f);
			holder.tags_layout.setProgressColor(mContext.getResources().getColor(
					R.color.text_orange));
			holder.tags_label.setText(investment.progress + "%");
			holder.tags_label.setBackgroundDrawable(null);

			if (!TextUtils.isEmpty(investment.itemTagIcon)) {
				Bitmap bmp = bitmapUtils.getBitmapFromMemCache(investment.itemTagIcon,
						displayConfig);

				if (bmp != null) {
					holder.tag_image.setImageBitmap(bmp);
				} else {
					bitmapUtils.display(holder.tag_image, investment.itemTagIcon, displayConfig);
				}
				holder.tag_image.setVisibility(View.VISIBLE);
			} else {
				holder.tag_image.setImageBitmap(null);
			}

			if ("full".equals(investment.itemStatus) || "repay".equals(investment.itemStatus)
					|| "success".equals(investment.itemStatus)) {
				holder.investment_rate.setTextColor(mContext.getResources().getColor(
						R.color.home_tab_text_normal));
				holder.investment_rate_sp.setTextColor(mContext.getResources().getColor(
						R.color.home_tab_text_normal));
				holder.investment_rate_tag.setTextColor(mContext.getResources().getColor(
						R.color.home_tab_text_normal));
				
				holder.investment_period.setTextColor(mContext.getResources().getColor(
						R.color.home_tab_text_normal));

				holder.type.setTextColor(mContext.getResources().getColor(
						R.color.home_tab_text_normal));
				holder.type.setBackgroundDrawable(mContext.getResources().getDrawable(
						R.drawable.shape_investment_type_s));

				holder.tags_layout_par.setEnabled(false);
				holder.tags_layout.setVisibility(View.INVISIBLE);
				holder.tags_label.setText("");
				holder.tags_label.setBackgroundDrawable(mContext.getResources().getDrawable(
						R.drawable.tou_lei_guang));
			} else if ("reserve".equals(investment.itemStatus)) {
				holder.tags_layout_par.setEnabled(true);

				holder.tags_label.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
				holder.tags_label.setText("敬请\n期待");
			} else if ("order".equals(investment.itemStatus)) {
				holder.tags_layout_par.setEnabled(true);

				holder.tags_label.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
				holder.tags_label.setText("立即\n预约");
			}
		}

		return convertView;
	}

	private void getRateView(Investment investment, ViewHolder holder){
		String rate_value = investment.rate;
		String rate = "";
		String rate_sp = "";
		String rate_tag = "";

		// 如果包含%， 删除所有%  手动添加
		rate_value = rate_value.contains("%") ? rate_value.replace("%", "") : rate_value;

		if (rate_value.contains("+")) {
			rate = rate_value.split("[+]")[0];
			rate_tag = rate_value.split("[+]")[1];

			if (rate.contains(".")) {
				rate_sp = rate.split("[.]")[1];
				rate = rate.split("[.]")[0]+".";
			}

			holder.investment_rate_tag.setVisibility(View.VISIBLE);

			holder.investment_rate.setText(rate);
			holder.investment_rate_sp.setText(rate_sp+"%");
			holder.investment_rate_tag.setText("+"+rate_tag+"%");
		} else {
			if (rate_value.contains(".")) {
				rate_sp = rate_value.split("[.]")[1];
				rate = rate_value.split("[.]")[0]+".";
			} else {
				rate = rate_value;
			}

			holder.investment_rate_tag.setVisibility(View.GONE);

			holder.investment_rate.setText(rate);
			holder.investment_rate_sp.setText(rate_sp+"%");
		}
	}

	class ViewHolder {
		// 头 title
		public TextView title;
		public TextView type;
		public ImageView tag_image;

		// 年化利率
		public TextView investment_rate;
		public TextView investment_rate_sp;
		public TextView investment_rate_tag;

		public TextView investment_period;

		// 圆形进度按钮
		FrameLayout tags_layout_par;
		public HomeSeekArc tags_layout;
		public TextView tags_label;
	}
}
